//
//  ViewController.m
//  TextView
//
//  Created by 周成龙 on 17/7/1.
//  Copyright © 2017年 ZCL. All rights reserved.
//

#import "ViewController.h"

#import "SKTextView.h"

#define Font(a)     [UIFont systemFontOfSize:a]
static NSString * const pattern   = @"\n- ";
@interface ViewController ()<UITextViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    SKTextView * textView = [[SKTextView alloc] initWithFrame:CGRectMake(10, 50, self.view.bounds.size.width - 20, 100)];
    
    textView.backgroundColor = [UIColor lightGrayColor];
    textView.layer.borderWidth = 1;
    textView.layer.borderColor = [[UIColor blackColor] colorWithAlphaComponent:0.5].CGColor;
    textView.textColor = [UIColor blackColor];
    textView.font = [UIFont systemFontOfSize:14];
    
    textView.placeholder = @"我就是个placeholder";
    textView.placeholderColor = [UIColor lightGrayColor];
    
    textView.delegate = self;
    
    [self.view addSubview:textView];
    
    // \n-
//    NSMutableParagraphStyle
}

- (void)textViewDidChange:(UITextView *)textView {
    
    NSRange lastRange = textView.selectedRange;
    NSString *text = textView.text;
    
    NSRegularExpression *regex = [NSRegularExpression
                                  regularExpressionWithPattern:pattern
                                  options:0
                                  error:nil];
    NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc]initWithString:text];
    NSArray *array1 = [regex matchesInString:text options:0 range:NSMakeRange(0, text.length)];
    NSLog(@"%ld",(long)[array1 count]);
    if ([array1 count]) {
        
        
//        for(int i = [array1 count]; i > 0; i--) {
//            NSTextCheckingResult *objc = array1[i];
//            [attribute addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(range.location, range.length)];
//            [attribute addAttribute:NSFontAttributeName value:Font(15.0f) range:NSMakeRange(range.location, range.length - 1)];
//            
//        }
        
        for (NSTextCheckingResult *objc in array1) {
            NSRange range = objc.range;
            
            [attribute addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(range.location, range.length)];
            [attribute addAttribute:NSFontAttributeName value:Font(15.0f) range:NSMakeRange(range.location, range.length - 1)];
        }
        textView.font = Font(15.0f);
        [attribute addAttribute:NSFontAttributeName value:Font(15.0f) range:NSMakeRange(0, text.length)];
        textView.attributedText = attribute;
    }else {
        [attribute addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, text.length)];
        [attribute addAttribute:NSFontAttributeName value:Font(15.0f) range:NSMakeRange(0, text.length)];
        textView.attributedText = attribute;
    }
    [textView setSelectedRange:lastRange];
}

//首行缩进
- (NSAttributedString *)paragraphStyleStart:(NSString *)text {
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.firstLineHeadIndent = 20.f;    /**首行缩进宽度*/
    paragraphStyle.alignment = NSTextAlignmentJustified;
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont systemFontOfSize:15],
                                 NSParagraphStyleAttributeName:paragraphStyle
                                 };
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    
    
    
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
